#coding=utf-8
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib import auth

from JaoeeProject.DocShare.models import catalog
from JaoeeProject.DocShare.forms import upFileForm

def login(request):
    username = request.POST.get('username',None)
    password = request.POST.get('password',None)
    user = auth.authenticate(username=username, password=password)#获取登录状态
    if user is not None and user.is_active:
        auth.login(request, user) #做登录处理
        request.session['username'] = username
        return HttpResponseRedirect('/resource/show_resource/')
    return render_to_response('permission/index.html')

def logout(request):
    auth.logout(request)
    return HttpResponseRedirect("/")

def show_resource(request):
    resource_list = list(catalog.objects.filter(parentId=0))#取根目录
    return render_to_response('resource/show_resource.html',{'resource_list':resource_list})

def show_leveltwo_resource(request,catogry):
    obj=catalog.objects.get(id=catogry)
    resource_list = list(catalog.objects.filter(parentId=catogry))
    return render_to_response('resource/show_leveltwo_resource.html',{'obj':obj,'resource_list':resource_list})

def ajax_leveltwo_resource(request,catogry):
    ajax_list = list(catalog.objects.filter(parentId=catogry))
    return HttpResponse(ajax_list)

def show_files(request,file_id,template_html):
    obj=catalog.objects.get(id=file_id)
    if request.method == 'POST':
        form = upFileForm(request.POST)
        if form.is_valid():
            print "sad"
    else:
        form = upFileForm()
    return render_to_response(template_html,{'obj':obj,'form':form})
